/***************************************************************************
                          main.c  -  description
                             -------------------
    copyright            : (C) 2002 by Duncan Mac-Vicar Prett
    email                : duncan@kde.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#include <math.h>
#include "main.h"
#include "hash.h"
#include "codigos.h"

extern entrada tabla[LARGO_TABLA];

extern int yylex();
extern char * yytext;
extern int yyleng;
extern FILE  *yyin;
int fila = 0, columna = 0;

/* Esto es para los sub-tipos, asi evitamos tener que
   o crear structs o comparar uno a uno */
int sub_code;
long int integer_value;

main( argc, argv )
int argc;
char **argv;
{
	int current_match;
	int table_index;

	/* Un poco de flexibilidad para poder pasar como parametro
		un archivo */

    ++argv, --argc;
    if ( argc > 0 )
	{
            yyin = fopen( argv[0], "r" );
    }
	else
	{
            yyin = stdin;
    }

    printf("\nPascal Lex, by Duncan Mac-Vicar P. 2002\n");
	printf("---------------------------------------\n\n");
	while ( current_match = yylex())
	{
    	switch (current_match)
		{
			/* Simples */
			case PROGRAM:
			case BEG:
			case END:
			case VAR:
			case ASSIGN:
			case RECORD:
			case INTEGER:
			case BOOLEAN:
            case CONST:
			case PROCEDURE:

				printf("[%3d             ] (%3d,%3d) %s\n",current_match, fila, columna - yyleng, yytext);
				break;
			/* Compuestos */
		    case OP_REL:
			case PUNCTUATION:
			case CONDITIONAL:
			case BUCLES:
			case BOOL:
			case OP_ADD:
			case OP_MUL:
				printf("[%3d,%12d] (%3d,%3d) %s\n",current_match, sub_code,  fila, columna - yyleng, yytext);
				break;
			case ID:
				table_index = busqueInserte( yytext );
				printf("[%3d,%12d] (%3d,%3d) %s\n", ID, table_index,fila, columna - yyleng, yytext);
				break;
			case INT:
				integer_value = atol( yytext );
				printf("[%3d,%12ld] (%3d,%3d) %s\n", ID, integer_value,fila, columna - yyleng, yytext);
				break;
			case UNKNOWN:
				printf("[Unknown Lex Error] (%3d,%3d) %s\n", UNKNOWN,fila, columna - yyleng, yytext);
				break;
				
		}
	}
}


/*
int main ()
{
	busqueInserte("int");
	busqueInserte("main");
	busqueInserte("hola");
    busqueInserte("main");

	return 0;
}
*/